package com.service.abolishing.controller;

import com.service.abolishing.dto.CheckTenantDto;
import com.service.abolishing.dto.TenantDto;
import com.service.abolishing.entity.CloudCampusInfo;
import com.service.abolishing.service.CloudCampusInfoService;
import com.service.abolishing.service.ManageInfoService;
import com.service.config.dto.ScreenUserDto;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.service.config.constant.Constant.*;

/**
 * openApi用户表(CloudCampusInfo)表控制层
 *
 * @author makejava
 * @since 2021-10-21 14:14:31
 */
@RestController
@RequestMapping("cloudCampusInfo")
@Api(value = "华为云理关联接口", tags = "华为云理关联接口")
public class CloudCampusInfoControllerOne {
    /**
     * 服务对象
     */
    @Resource
    private CloudCampusInfoService cloudCampusInfoService;
    @Resource
    private ManageInfoService manageInfoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 校验云管平台的账号和密码
     * @param cloudAuthToken
     * @param checkTenantDto
     * @return
     */
    @PostMapping("checkApiAccount")
    @ApiOperation(value = "校验云管平台的账号和密码")
    @ResponseBody
    public Result checkTenant(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                              @RequestBody CheckTenantDto checkTenantDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                Map check = cloudCampusInfoService.check(checkTenantDto, userId);
                if (check.containsKey("errorMsg")) {
                    return Result.ok().setData(check);
                }
                return Result.ok().setData(new HashMap<>());
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 增加云管api账号和密码
     * @param cloudAuthToken
     * @param tenantDto
     * @return
     * @throws Exception
     */
    @PostMapping("addApiAccount")
    @ApiOperation(value = "增加云管api账号和密码")
    @ResponseBody
    public Result addApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                @RequestBody TenantDto tenantDto) throws Exception {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<CloudCampusInfo> screenUserList = cloudCampusInfoService.insertOrUpdate(ModelMapperUtil.strictMap(tenantDto, CloudCampusInfo.class), userId);
                if (screenUserList != null) {
                    return Result.failure(CLOUD_CAMPUS_ACCOUNT_SAME, "云管理账号同域名已存在，是否覆盖");
                }
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 更新云管的密码
     * @param cloudAuthToken
     * @param tenantDto
     * @return
     */
    @PostMapping("updateApiAccount")
    @ApiOperation(value = "更新云管的密码")
    @ResponseBody
    public Result updateApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestBody TenantDto tenantDto) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudCampusInfoService.updateApiAccount(ModelMapperUtil.strictMap(tenantDto,CloudCampusInfo.class));
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 删除云管理api账号
     * @param cloudAuthToken
     * @param screen_id
     * @return
     */
    @GetMapping("deleteApiAccount")
    @ApiOperation(value = "删除云管理api账号")
    @ResponseBody
    public Result deleteApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("screen_id") @ApiParam("api账号id") String screen_id) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudCampusInfoService.deleteById(screen_id);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * api账号的启用和禁用
     * @param cloudAuthToken
     * @param screen_user_id
     * @param status
     * @param domain
     * @return
     */
    @GetMapping("enableAndDisable")
    @ApiOperation(value = "api账号的启用和禁用")
    @ResponseBody
    public Result enableAndDisable(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("screen_user_id") @ApiParam("大屏用户id") String screen_user_id,
                                   @ApiParam("状态：true-启用，false-禁用")@RequestParam("status") Boolean status,
                                   @ApiParam("域名")@RequestParam("domain") String domain) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudCampusInfoService.serviceStatusBind(screen_user_id,status,userId,domain);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    /**
     * 云管账号列表
     * @param cloudAuthToken
     * @return
     */
    @GetMapping("findAllApi")
    @ApiOperation(value = "云管账号列表")
    @ResponseBody
    public Result findAllApi(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<ScreenUserDto> screenUserList = cloudCampusInfoService.queryAll(userId);
                return Result.ok().setData(screenUserList);
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @GetMapping("findSecret")
    @ApiOperation(value = "查看密钥")
    @ResponseBody
    public Result findSecret(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                             @RequestParam("screen_id") @ApiParam("api账号id") String screen_id) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                CloudCampusInfo cloudCampusInfo = cloudCampusInfoService.queryById(screen_id);
                return Result.ok().setData(ModelMapperUtil.strictMap(cloudCampusInfo, ScreenUserDto.class));
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @GetMapping("freshSecret")
    @ApiOperation(value = "刷新密钥")
    @ResponseBody
    public Result freshSecret(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                              @RequestParam("screen_id") @ApiParam("api账号id") String screen_id) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                CloudCampusInfo cloudCampusInfo = cloudCampusInfoService.freshSecret(userId);
                return Result.ok().setData(ModelMapperUtil.strictMap(cloudCampusInfo, ScreenUserDto.class));
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    /**
     * 更新校验参数
     * @param cloudAuthToken
     * @return
     */
    @PostMapping("updateParam")
    @ApiOperation(value = "更新校验参数")
    @ResponseBody
    public Result updateValidator(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                  @RequestBody TenantDto tenantDto) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudCampusInfoService.updateValidator(tenantDto.getId(),tenantDto.getValidator());
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
